/*
 * 文件名：CommentaryController.java
 * 版权：Copyright 2007-2015 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： CommentaryController.java
 * 修改人：yanshu
 * 修改时间：2015年12月25日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.feichuang.chineseWoodCarving.business.ICommentaryBusiness;
import com.feichuang.chineseWoodCarving.model.MCommentary;
import com.feichuang.chineseWoodCarving.model.MUser;
import com.google.gson.Gson;

/**
 * TODO 评论操作控制Controller.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Controller
@RequestMapping(value = "/commentary")
public class CommentaryController {

    /**
     * 
     */
    @Resource(name = "commentaryBusiness")
    ICommentaryBusiness commentaryBusiness;

    /**
     * 
     * TODO 发表评论,需要检查权限,只有会员才能评论.
     * 
     * @param request
     * @param response
     * @param param
     * @param type
     * @return
     */
    @RequestMapping(value = "/pushCommentary", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String putCommentary(HttpServletRequest request, HttpServletResponse response, String param, String type) {

        Gson gson = new Gson();
        Response<String> res = new Response<String>();
        MUser userInfo = (MUser) request.getSession().getAttribute("LoginStaff");
        if (null == userInfo) {
            res.setCode(StatusCode.BAD_REQUEST);
            res.setMessage("请登录后才能使用此功能");
        } else {
            MCommentary commentary = gson.fromJson(param, MCommentary.class);
            if (commentaryBusiness.insertCommentary(commentary)) {
                res.equals(StatusCode.SUCCESS);
                res.setMessage("回复成功");
            } else {
                res.equals(StatusCode.ERROR);
                res.setMessage("风速过大");
            }
        }
        return gson.toJson(res);
    }

    /**
     * 
     * TODO 删除评论.
     * 
     * @param request
     * @param response
     * @param param
     * @param type
     * @return
     */
    @RequestMapping(value = "/pushCommentary", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteCommentary(HttpServletRequest request, HttpServletResponse response, String param, String type) {

        Gson gson = new Gson();
        Response<String> res = new Response<String>();
        MUser userInfo = (MUser) request.getSession().getAttribute("LoginStaff");
        if (null == userInfo) {
            res.setCode(StatusCode.BAD_REQUEST);
            res.setMessage("请登录后才能使用此功能");
        } else {
            MCommentary commentary = gson.fromJson(param, MCommentary.class);
            if (userInfo.getIsAdmin() != 1) {
                res.equals(StatusCode.BAD_REQUEST);
                res.setMessage("您没有管理员权限,不能执行此操作");
            }
            if (commentaryBusiness.deleteMCommentary(commentary)) {
                res.equals(StatusCode.SUCCESS);
                res.setMessage("删除成功");
            } else {
                res.equals(StatusCode.ERROR);
                res.setMessage("风速过大");
            }
        }
        return gson.toJson(res);
    }

}
